# cython: language_level=3
import hashlib


def split_by_cnt(split_value, splits):
    """
    根据数据分割
    :param split_value:
    :param splits:
    :return:
    """
    try:
        r = split_value % 10
        if r in splits:
            return 1
        else:
            return 0
    except Exception as e:
        print('error:', e)
        return 0


def split_ab_test(split_key_value, splits, key_head):
    """
    利用md5进行abtest分区
    :param split_key_value:
    :param splits:
    :return:
    """
    try:
        v_org = str(key_head) + str(split_key_value)
        v = v_org.encode('utf-8')
        hash_v = hashlib.md5(v).hexdigest()
        as_int = int(hash_v, 16)
        r = as_int % 10
        if r in splits:
            return 1
        else:
            return 0
    except Exception as e:
        print('error:', e)
        return 0


if __name__ == '__main__':

    one = 0
    zero = 0
    for i in range(1, 100):
        split_result = split_ab_test(i, [0, 2, 4, 6, 8], 'inloan121ewq12')
        print(i, split_result)
        if split_result == 0:
            zero += 1
        else:
            one += 1
    print(zero, one)

    v = str(23123).encode('utf-8')
    hash_v = hashlib.md5(v).hexdigest()
    as_int = int(hash_v, 16)
    print(as_int % 10)

    result = split_ab_test(
        split_key_value='NIGERIA#1#com.rapidnaira.fastloan#2e1857ae-7c2d-4a1a-99d4-94436f885253#1477173363372851200',
        splits=[0, 1, 2], key_head='fee_abtest')
    print(result)
